package com.lagou.dao.impl;

import com.lagou.dao.CourseContentDao;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 10:46 上午 2021/6/11
 */
public class CourseContentDaoImpl implements CourseContentDao {

    @Override
    public List<Course_Section> findSectionByCourseId(int id) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_name,description,order_num,`status`,create_time,update_time,is_del FROM course_section WHERE course_id=?";

            List<Course_Section> list = queryRunner.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), id);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Course_Lesson> findLessonByCourseID(int id) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_id,theme,duration,is_free,order_num,`status`,create_time,update_time,is_del FROM course_lesson WHERE course_id=?";

            List<Course_Lesson> list = queryRunner.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), id);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public Course findCourseById(int course_id) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT id,course_name from course WHERE id=?";
            Course course = queryRunner.query(sql, new BeanHandler<Course>(Course.class), course_id);
            return course;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int saveSection(Course_Section courseSection) {
        try {
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            String sql="INSERT INTO course_section (course_id,section_name,description,order_num,create_time,update_time) VALUES(?,?,?,?,?,?)";
            Object[] param={courseSection.getCourse_id(),courseSection.getSection_name(),courseSection.getDescription(),courseSection.getOrder_num(),courseSection.getCreate_time(),courseSection.getUpdate_time()};
            int row= queryRunner.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateSection(Course_Section courseSection) {
        try {
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            String sql="UPDATE course_section SET course_id=?,section_name=?,description=?,order_num=?,update_time=? WHERE id=?";
            Object[] param={courseSection.getCourse_id(),courseSection.getSection_name(),courseSection.getDescription(),courseSection.getOrder_num(),courseSection.getUpdate_time(),courseSection.getId()};
            int row = queryRunner.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public int updateSectionStatus(Course_Section courseSection) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "UPDATE course_section SET STATUS = ? ,update_time = ? WHERE id = ?";

            Object[] param = {courseSection.getStatus(),courseSection.getUpdate_time(),courseSection.getId()};

            int row = qr.update(sql, param);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int saveLesson(Course_Lesson courseLesson) {
        try {
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());

            String sql="INSERT INTO course_lesson (course_id,section_id,theme,duration,is_free,order_num,create_time,update_time) VALUES(?,?,?,?,?,?,?,?)\t";

            Object[] param={courseLesson.getCourse_id(),courseLesson.getSection_id(),courseLesson.getTheme(),courseLesson.getDuration(),courseLesson.getIs_free(),courseLesson.getOrderNum(),courseLesson.getCreate_time(),courseLesson.getUpdate_time()};

            int row = queryRunner.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateLesson(Course_Lesson courseLesson) {
        try {
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());

            String sql="UPDATE course_lesson SET course_id=?,section_id=?,theme=?,duration=?,is_free=?,order_num=?,update_time=? WHERE id=?";

            Object[] param={courseLesson.getCourse_id(),courseLesson.getSection_id(),courseLesson.getTheme(),courseLesson.getDuration(),courseLesson.getIs_free(),courseLesson.getOrderNum(),courseLesson.getUpdate_time(),courseLesson.getId()};

            int row = queryRunner.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
